// routine for manage visibility states according to buttons actions
function columnHiderButtonActions(isExpand, selectorsId, collapseButtonId, expandButtonId, stateFld) {
	// save state
	document.getElementById(stateFld).value = isExpand ? 'expanded' : 'collapsed';
	// hide/show selectors/buttons
	columnHiderButtonActionsSupply(isExpand, selectorsId, collapseButtonId, expandButtonId);
}

// restore states of checkers/columns after page reload
// countOfRecods - should be specified for SelectMany tables ONLY!
function columnHiderRestoreState(tableHaveSelectors, selectorsId, headerTableId, contentTableId, visibilityState, collapseButtonId, expandButtonId, supplyFld, countOfRecods) {
	if (document.getElementById(selectorsId) == null) return;
	var haveSelectManyHeader = false;
	if (countOfRecods !== undefined) {
		haveSelectManyHeader = countOfRecods != 0;
	}
	columnHiderButtonActionsSupply(visibilityState == 'expanded', selectorsId, collapseButtonId, expandButtonId);
	var checkBoxes = document.getElementById(selectorsId).getElementsByTagName('input');
	columnHiderFixupDisableVisualization(selectorsId, supplyFld);
	for (var i = 0; i < checkBoxes.length; i++) {
		columnHiderHideShowColumns(checkBoxes[i], tableHaveSelectors, headerTableId, contentTableId, haveSelectManyHeader);
	}
	// fixup columns visualization
	columnHiderFixupColumnsVisualization(headerTableId, contentTableId, haveSelectManyHeader);
}

// operate with changes of checkers states
//countOfRecods - should be specified for SelectMany tables ONLY!
function columnHiderChangeState(checkerObj, tableHaveSelectors, selectorsId, headerTableId, contentTableId, supplyFld, countOfRecods) {
	var haveSelectManyHeader = false;
	if (countOfRecods !== undefined) {
		haveSelectManyHeader = countOfRecods != 0; 
	}	
	// show/hide columns
	columnHiderHideShowColumns(checkerObj, tableHaveSelectors, headerTableId, contentTableId, haveSelectManyHeader);
	// fixup displaying last stand checker
	columnHiderFixupDisableVisualization(selectorsId, supplyFld);
	// fixup columns visualization
	columnHiderFixupColumnsVisualization(headerTableId, contentTableId, haveSelectManyHeader);
}

//supply routine (don not call it directly from jsp)
function columnHiderButtonActionsSupply(isExpand, selectorsId, collapseButtonId, expandButtonId) {
	// hide/show selectors
	document.getElementById(selectorsId).parentNode.style.display = isExpand ? '' : 'none';
	// hide/show buttons
	document.getElementById(collapseButtonId).parentNode.style.display = isExpand ? '' : 'none';
	document.getElementById(expandButtonId).parentNode.style.display = isExpand ? 'none' : '';
}

//supply routine (don not call it directly from jsp)
function columnHiderHideShowColumns(checkerObj, tableHaveSelectors, headerTableId, contentTableId, haveSelectManyHeader) {
	var startIndex = tableHaveSelectors ? 1 : 0;
	var operatableIndex = startIndex + (+checkerObj.value);
	var tblContentOffset = haveSelectManyHeader ? 1 : 0;
	// operate with header
	var tblHeader = document.getElementById(headerTableId).getElementsByTagName("table")[0].getElementsByTagName("table")[0].getElementsByTagName("tr")[0].getElementsByTagName("th");
	tblHeader[operatableIndex].style.display = checkerObj.checked ? '' : 'none';
	// operate with content
	var tblContent = document.getElementById(contentTableId).getElementsByTagName("table")[0].getElementsByTagName("table")[tblContentOffset];
	var tblContentHeader = tblContent.getElementsByTagName("tr")[0].getElementsByTagName("th");
	tblContentHeader[operatableIndex].style.display = checkerObj.checked ? '' : 'none';
	var tblContentContent = tblContent.getElementsByTagName("tbody")[0].childNodes; // childNodes --> getElementsByTagName("tr");
	for (var i = 1; i < tblContentContent.length; i++) {
		// childNodes --> getElementsByTagName("td")
		tblContentContent[i].childNodes[operatableIndex].style.display = checkerObj.checked ? '' : 'none';
	}
}

//supply routine (don not call it directly from jsp)
function columnHiderFixupColumnsVisualization(headerTableId, contentTableId, haveSelectManyHeader) {
	var tblContentOffset = haveSelectManyHeader ? 1 : 0;
	var tblHeader = document.getElementById(headerTableId).getElementsByTagName("table")[0].getElementsByTagName("table")[0].getElementsByTagName("tr")[0].getElementsByTagName("th");
	var tblContent = document.getElementById(contentTableId).getElementsByTagName("table")[0].getElementsByTagName("table")[tblContentOffset].getElementsByTagName("tr")[0].getElementsByTagName("th");
	// fixup columns visualization
	for (var i = 0; i < tblHeader.length; i++) {
		var thHeader = tblHeader.item(i);
		var thContent = tblContent.item(i);
		if (thHeader.style.display != "none") {
			var width = thHeader.offsetWidth - 3;
			var newHTML = "<img border='0' src='image/1x1_transparent.png' width='" + width + "' height='1'/>";
			thContent.innerHTML = newHTML;
		}
	}
}

// supply routine (don not call it directly from jsp)
function columnHiderFixupDisableVisualization(selectorsId, supplyFld) {
	var countOfSelectedSelectors = 0;
	var tempValue;
	var supplyObj = document.getElementById(supplyFld);
	var checkBoxes = document.getElementById(selectorsId).getElementsByTagName('input');
	for (var i = 0; i < checkBoxes.length; i++) {
		if (checkBoxes[i].checked) {
			countOfSelectedSelectors++;
			tempValue = checkBoxes[i].value; 
		}	
	}
	if (countOfSelectedSelectors == 1) {
		// only one selector selected - make it read only
		for (i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].checked) {
				checkBoxes[i].disabled = true;
				break;
			}
		}
	} else if (countOfSelectedSelectors == 0) {
		// no selectors active --> restore selectors
		for (i = 0; i < checkBoxes.length; i++) {
			if (checkBoxes[i].value == supplyObj.value) {
				checkBoxes[i].checked = true;
				checkBoxes[i].disabled = true;
				break;
			}
		}
		tempValue = supplyObj.value;
	}else {
		// more than one selected - enable all
		for (i = 0; i < checkBoxes.length; i++) {
			checkBoxes[i].disabled = false;
		}
	}
	supplyObj.value = tempValue;
}